package lfm.mpr;

import java.net.SocketTimeoutException;
import java.util.ArrayList;


import db.AdminBaseDatosHelper;
import objects.Equipo;
import objects.Liga;
import Soap.Connection_Sever_Dynamics;
import Toast.ToastLFM_Message;
import adapter.AdapterListMyTeams;
import adapter.AdapterListUser;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MyTeamsLFMActivity extends Activity{
	
	 private ArrayList<Equipo> list_equipo;
	 private ListView list_equipo_LFM;
	 private SharedPreferences prefs;
	 private AdminBaseDatosHelper miBBDDHelper;
	 private AdapterListMyTeams adapter_myteams;
	 public static Equipo team;
	 boolean customTitleSupported;
	
	
	 public void onCreate(Bundle savedInstanceState) {
		 
	        super.onCreate(savedInstanceState);
	        customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
	        setContentView(R.layout.list_myteams_liga);
	        customTitleBar(getText(R.string.app_name).toString());
	        
	        prefs = getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);
	        
	        list_equipo_LFM = (ListView)findViewById(R.id.listView_mytemas_lfm);
	        list_equipo_LFM.setFastScrollEnabled(true); 
	        int[] colors = {0, 0xFFFF0000, 0}; // red for the example
	        list_equipo_LFM.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
	        list_equipo_LFM.setDividerHeight(3);
	        
	        miBBDDHelper = new AdminBaseDatosHelper(MyTeamsLFMActivity.this);
	        list_equipo = miBBDDHelper.Get_MyTeams_BD();
	        
	        adapter_myteams = new AdapterListMyTeams(MyTeamsLFMActivity.this, list_equipo);
	        list_equipo_LFM.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	        list_equipo_LFM.setAdapter(adapter_myteams);
	        adapter_myteams.notifyDataSetChanged();
	        
	        if(list_equipo.isEmpty()){
	        	ToastLFM_Message.Show_Alert("Aviso", "Crear tus propios Equipos \n, registralos en una liga y a ganar!", MyTeamsLFMActivity.this);
	        	new Handler().postDelayed(new Runnable() { 
	        		   public void run() { 
	        		     openOptionsMenu(); 
	        		   } 
	        		}, 1000); 
	        }
	        
	        
	 }//end_onCreate
	 
	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	    	 Intent intent_home = new Intent(MyTeamsLFMActivity.this, LfmActivity.class);
	    	 startActivity(intent_home);
	    }
	    return super.onKeyDown(keyCode, event);
	 }
	 
	 @Override
	 public boolean onCreateOptionsMenu(Menu menu) {
	     MenuInflater inflater = getMenuInflater();
	     inflater.inflate(R.menu.menu_myteams, menu);
	     return true;
	 }
	 
	 
	 @Override
	 public boolean onOptionsItemSelected(MenuItem item) {
	     switch (item.getItemId()) {
	         case R.id.newteam:     
	        		Intent intent = new Intent(MyTeamsLFMActivity.this, TabHost_Team.class);
	        		TabHost_Team.Mode = "New";
	        		TabHost_Team.team = null;
             		startActivity(intent);
	              break;
	     }
	     return true;
	 }
	 

	 public void customTitleBar(String left) {
			
			//Miramos las preferencias de usuario si existen
			SharedPreferences prefs = getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);
	        String user = prefs.getString("user", "lfm");
	        
	        if (user.length() > 20) user = user.substring(0, 20);
	        // set up custom title
	        if (customTitleSupported) {
	                getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titleapp);
	                TextView titleTvLeft = (TextView) findViewById (R.id.left_text);
	                titleTvLeft.setText(user);
	                titleTvLeft.setTextColor(Color.WHITE);
	        }
		}//end_customTitleBar


}//end_MyTeamsLFMActivity
